<?php

class QuestionController extends Zend_Controller_Action
{

    protected $_authNamespace;
    protected $_redirector = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->_authNamespace = new Zend_Session_Namespace('Zend_Auth');
        $this->_redirector = $this->_helper->getHelper('Redirector');
    }

/*    public function indexAction()
    {
        if($this->_authNamespace->userRole == 'admin') {
            $question = new Application_Model_QuestionMapper();
            $this->view->entries = $question->fetchAll();
        } else {
            return $this->_helper->redirector->gotoUrl('/auth/login');
        }
    }
*/

    public function addQuestionAction()
    {
        if($this->_authNamespace->userRole == 'admin') {
            $questionnaire_id = $this->_getParam("questionnaire_id");
            $request = $this->getRequest();
            $form    = new Application_Form_Question();
            $form_data = array("questionnaire" => $questionnaire_id);
            $form->populate($form_data);

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $question = new Application_Model_Question($form->getValues());
                    $questionnaire_id = $question->getQuestionnaire();
                    $mapper  = new Application_Model_QuestionMapper();
                    $mapper->save($question);
                    
                    $this->_redirector->setGotoUrl('/questionnaire/edit-questionnaire/?questionnaire_id='.$questionnaire_id);
                    //return $this->_helper->redirector->setGotoSimple("edit-questionnaire",
                    //                      "some-controller");
                                          //('../questionnaire/edit-questionnaire/index?questionnaire_id='.$questionnaire_id);
                }
            }
     
            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function editQuestionAction()
    {
        if($this->_authNamespace->userRole == 'admin') {
            $question_id = $this->_getParam("question_id");
            $request = $this->getRequest();
            $form    = new Application_Form_Question();
            $this->view->question_id = $question_id;

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $data = array("id" => (int)$question_id);
                           
                    $question = new Application_Model_Question($data);
                    $questionnaire_id = $form->getValue('questionnaire');
                    $mapper  = new Application_Model_QuestionMapper();
                    $mapper->find($question_id, $question);
                    $question->setQuestion($form->getValue('question'));
                    $question->setQuestionnaire($form->getValue('questionnaire'));
                    $mapper->save($question);
                    
                    $this->_redirector->setGotoUrl('/questionnaire/edit-questionnaire/?questionnaire_id='.$questionnaire_id);
                }
            } else {
                $data = array("id" => (int)$question_id);
                           
                $question = new Application_Model_Question($data);

                $mapper  = new Application_Model_QuestionMapper();
                $mapper->find($question_id, $question);
                $form_data = array("id" => $question->getId(), "question" => $question->getQuestion(), "questionnaire" => $question->getQuestionnaire());
                $form->populate($form_data);
            }
     
            
            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function deleteAction()

    {
        if($this->_authNamespace->userRole == 'admin') {
            $question_id = $this->_getParam("question_id");
            $this->view->idValue = $question_id;
            if ($question_id != null) {
                $data = array("id" => (int)$question_id);
                           
                $question = new Application_Model_Question($data);
                
                
                $mapper  = new Application_Model_QuestionMapper();
                
                $quest = new Application_Model_Question();
                $mapper->find((int)$question_id, $quest);
                $questionnaire_id = $quest->getQuestionnaire();
                
                $mapper->delete($question);
                
                $this->_redirector->setGotoUrl('/questionnaire/edit-questionnaire/?questionnaire_id='.$questionnaire_id);
            }

        } else {
            $this->view->message = "You don't have permissions to do this!";
            return $this->_helper->redirector('index');
        }
    }



}



